package com.css.fxfzdzzh.modules.spatialData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.spatialData.repository.entity.BoundaryEntity;
import com.css.fxfzdzzh.modules.spatialData.service.BoundaryService;
import com.css.fxfzdzzh.modules.spatialData.viewobjects.BoundaryQueryParams;
import com.css.fxfzdzzh.util.AreaBoundaryUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author andy
 * @date 2020-12-23
 */
@Slf4j
@RestController
@RequestMapping("/boundary")
public class BoundaryController {

    @Autowired
    private BoundaryService boundaryService;

    @GetMapping("/getSingleProvinceBoundary")
    public RestResponse getSingleProvinceBoundary(BoundaryQueryParams queryParams) {
        RestResponse response = null;
        try {
            JSONObject jsonObject = boundaryService.getBoundaries(queryParams);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getSingleCityBoundary")
    public RestResponse getSingleCityBoundary(BoundaryQueryParams queryParams) {
        RestResponse response = null;
        try {
            String city = queryParams.getCity();
            if ("北京市".equals(city) || "重庆市".equals(city)) city = "市辖区";
            JSONObject jsonObject = boundaryService.getBoundaries(queryParams);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/getSingleAreaBoundary")
    public RestResponse getSingleAreaBoundary(BoundaryQueryParams queryParams) {
        RestResponse response = null;
        try {
            String city = queryParams.getCity();
            if ("北京市".equals(city) || "重庆市".equals(city)) city = "市辖区";
            queryParams.setCity(city);
            JSONObject jsonObject = boundaryService.getBoundaries(queryParams);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }





    @GetMapping("/getCitiesBoundaryByProvince")
    public RestResponse getProvinceBoundary(@RequestParam("provinceName") String provinceName) {
        RestResponse response = null;
        try {
            Map<String, String> city2boundary = AreaBoundaryUtil.getAllCitiesBoundaryByProvince(provinceName);
            response = RestResponse.succeed(city2boundary);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getAreasBoundaryByProvinceAndCity")
    public RestResponse getAreasBoundaryByProvinceAndCity(@RequestParam("provinceName") String provinceName, @RequestParam("cityName") String cityName) {
        RestResponse response = null;
        try {
            Map.Entry<String, Map<String, String>> coords = AreaBoundaryUtil.getCoordsByCityName(provinceName, cityName);
            response = RestResponse.succeed(coords);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /*@GetMapping("/isPolygonsIntersect")
    public RestResponse isPolygonsIntersect(@RequestParam("coords") String coords) {
        RestResponse response = null;
        try {
            String[] lon_lats = StringUtils.split(coords, ";");
            Point[] cPoints = new Point[lon_lats.length];
            for (int i = 0; i < lon_lats.length; i++) {
                String[] lon_lat = StringUtils.split(lon_lats[i], ",");
                Point point = new Point();
                point.x = Double.parseDouble(lon_lat[0]);
                point.y = Double.parseDouble(lon_lat[1]);
                cPoints[i] = point;
            }

            Set<String> areas = new HashSet<>();
            Map<String, Map<String, Map<String, String>>> province_city2boundary = AreaBoundaryUtil.dataStore;
            for(Map.Entry<String, Map<String, Map<String, String>>> pEntry : province_city2boundary.entrySet()){
                Map<String, Map<String, String>> pb = pEntry.getValue();
                for(Map.Entry<String, Map<String, String>> cEntry : pb.entrySet()){
                    Map<String, String> ab = cEntry.getValue();
                    for(Map.Entry<String, String> aEntry : ab.entrySet()){
                        String coordinate = aEntry.getValue();
                        String[] coordinates = StringUtils.split(coordinate, "|");
                        for (int j = 0; j < coordinates.length; j++) {
                            String[] lonlats = StringUtils.split(coordinates[j],";");
                            Point[] points = new Point[lonlats.length];
                            for (int i = 0;i < lonlats.length;i++) {
                                String[] lonlat = StringUtils.split(lonlats[i], ",");
                                Point point = new Point();
                                point.x = Double.parseDouble(lonlat[1]);
                                point.y = Double.parseDouble(lonlat[0]);
                                points[i] = point;
                            }
                            if (AreaBoundaryUtil.isPointInPolygonBidirectional(cPoints, points)) {
                                areas.add(pEntry.getKey() + "-" + cEntry.getKey() + "-" + aEntry.getKey());
                                break;
                            }
                        }
                    }
                }
            }
            response = RestResponse.succeed(areas);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }*/

    @GetMapping("/getBoundaryEntityByCode")
    public RestResponse getBoundaryEntityByCode(String code) {
        RestResponse response = null;
        try {
            if (StringUtils.isNotBlank(code)){
                BoundaryEntity boundaryEntity = boundaryService.getBoundaryEntityByCode(code);
                response = RestResponse.succeed(boundaryEntity);
            }else {
                response = RestResponse.fail("参数code为空");
            }
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

}